package com.collect.zambia.zhiBase;

import android.app.Activity;
import android.content.Context;

import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BaseActivityManager {
    private static BaseActivityManager baseActivityManager;

    private List<BaseActivity> activityStack;

    public BaseActivityManager() {
        activityStack = new ArrayList<>();
    }

    public static BaseActivityManager getInstance() {
        if (null == baseActivityManager) {
            baseActivityManager = new BaseActivityManager();
        }
        return baseActivityManager;
    }

    /**
     * 添加activity
     */
    public void addActivity(BaseActivity activity) {
        if (null != activityStack) {
            activityStack.add(activity);
        }
    }

    /**
     * 移除一个activity
     */
    public void removeActivity(BaseActivity activity) {
        if (null != activityStack) {
            activityStack.remove(activity);
        }
    }

    /**
     * 关闭所有的activity
     */
    public void finishAllActivity() {
        if (null != activityStack) {
            for (BaseActivity activity : activityStack) {
                activity.finish();
            }
        }
    }

    /**
     * 关闭之外的所有的activity
     */
    public void finishExcludeActivity(Class c) {
        if (null != activityStack) {
            BaseActivity exclude = getActivity(c);
            for (BaseActivity activity : activityStack) {
                if (exclude != activity) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 获取目标actiivty
     */
    public <T extends BaseActivity> T getActivity(Class c) {
        if (null != activityStack) {
            return (T) activityStack.get(getActivityAt(c));
        }
        return null;
    }

    /**
     * 获取目标Activity所在位置
     */
    public int getActivityAt(Class c) {
        if (null != activityStack) {
            for (int i = 0; i < activityStack.size(); i++) {
                if (activityStack.get(i).getClass() == c) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 判断目标类是否正在运行
     */
    public boolean isRun(Class c) {
        return getActivityAt(c) != -1;
    }

    /**
     * 判断该类是不是在类顶部
     */
    protected static boolean isTopActivity(Activity activity) {
        android.app.ActivityManager activityManager = (android.app.ActivityManager) activity
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<android.app.ActivityManager.RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
        if (tasksInfo.size() > 0) {
            // 应用程序位于堆栈的顶层
            assert tasksInfo.get(0).topActivity != null;
            return ZHIProjectConfigs.PACKAGE_NAME.equals(Objects.requireNonNull(tasksInfo.get(0).topActivity).getPackageName());
        }
        return false;
    }

    public BaseActivity getCurrActivity() {
        if (activityStack == null || activityStack.size() == 0) {
            return null;
        } else {
            return activityStack.get(activityStack.size() - 1);
        }
    }

    /*
     * 判断是否只有一个activity
     */
    public boolean isOnly() {
        if (null != activityStack) {
            return activityStack.size() == 1;
        }
        return false;
    }
}